<?php
class Admin_GarageController extends SedController 
{
	public function indexAction(){
		$smarty = FRM::load('smarty');
		Customer::checkPermission();
		$cur_login_user = unserialize(FRM_Session::get('user_serialize'));

		$filter['customer_id'] = $cur_login_user->id;
		$modelGarage = new Garage();
		$garage = $modelGarage->getList($filter);
		
		if(FRM_Request::get('action') == 'delete'){
			$modelGarage->get(FRM_Request::get('id'));
			$modelGarage->delete();
			FRM::redirect(APPLICATION_URL.'garage');
		}
		$url_base = APPLICATION_URL.'/garage/index';
		$smarty->assign('url_base', $url_base);
		$smarty->assign('garage', $garage);
		$smarty->assign('current_user', $cur_login_user);
		$smarty->display('public/garage.html');
	}
	
	public function getmarkerAction(){
		$lat = trim($_POST['lat']);
		$lng = trim($_POST['lng']);
		$radius = 25;
		$filter = array();
		$filter['lat'] = $lat;
		$filter['lng'] = $lng;
		$filter['radius'] = $radius;
		
		$modelGarage = new Garage();
		$garage = $modelGarage->getGarageNearBy($filter, false);
		$arrGarage = array();
		while($garage->fetch()){
			$arrGarage[] = array('title' => $garage->title, 'category' => $garage->category_name, 'link' => $garage->link, 'm2' => $garage->m2, 'meter' => $garage->meter, 'spot' => $garage->spot, 'lat' => $garage->latitude, 'lng' => $garage->longitude);
		}
		
		print_r(json_encode($arrGarage));die;
	} 
	
	public function addAction(){
		$smarty = FRM::load('smarty');
		 
		Customer::checkPermission();
		$cur_login_user = unserialize(FRM_Session::get('user_serialize'));
		 
		$modelCat = new Category();
		$category = $modelCat->getList();
		$stroption = '';
		while($category->fetch()){
			$stroption .= '<option value="'.$category->id.'">'.$category->name.'</option>';
		}
		$filter['customer_id'] = $cur_login_user->id;
	
		$smarty->assign('str_cat_option', $stroption);
		$smarty->assign('current_user', $cur_login_user);
		$smarty->display('public/step2.html');
	}	
	
	public function insertAction(){
		$lng = trim($_POST['lng']);
		$lat = trim($_POST['lat']);
		$title = trim($_POST['title']);
		$category_id = trim($_POST['category_id']);
		$m2 = trim($_POST['m2']);
		$meter = trim($_POST['meter']);
		$spot = trim($_POST['spot']);
		$address = trim($_POST['address']);
		$cur_login_user = unserialize(FRM_Session::get('user_serialize'));
		 
		$garage = new Garage();
		$garage->title = $title;
		$garage->category_id = $category_id;
		$garage->m2 = $m2;
		$garage->meter = $meter;
		$garage->spot = $spot;
		$garage->longitude = $lng;
		$garage->latitude = $lat;
		$garage->address = $address;
		$garage->customer_id = $cur_login_user->id;
		 
		if($garage->insert()){
			return true;
		}else{
			return false;
		}
	}	
	
	public function packageAction(){
		$smarty = FRM::load('smarty');
		Customer::checkPermission();
		$cur_login_user = unserialize(FRM_Session::get('user_serialize'));

		$filter['customer_id'] = $cur_login_user->id;
		$modelGarage = new Garage();
		$garage = $modelGarage->getList($filter, false);
		 
		$modelpack = new Package();
		$package = $modelpack->getList(false);
		 
		$modelAd = new Advertisement();
		$advertise = $modelAd->getList(false);
		 
		if(FRM_Request::get('act')){
	
			$modelGarage = new Garage();
			$garage_id = FRM_Request::getIntNumber('garage');
			$modelGarage->get($garage_id);
			if($link = FRM_Request::getString('link')){
				$modelGarage->link = $link;
			}
			if($package = FRM_Request::getString('package')){
				$modelGarage->package_id = $link;
			}
			if($advertise = FRM_Request::getString('advertise')){
				$modelGarage->ad_id = $advertise;
			}
			if($desc = FRM_Request::getString('desc')){
				$modelGarage->description = $desc;
			}
			$modelGarage->update();
	
			FRM::redirect(APPLICATION_URL.'garage');
		}
		 
		$smarty->assign('garage', $garage);
		$smarty->assign('package', $package);
		$smarty->assign('advertise', $advertise);
		$smarty->assign('current_user', $cur_login_user);
		$smarty->display('public/step3.html');
	}
	
	public function viewAction(){
		$smarty = FRM::load('smarty');
		
		$garage_id  = trim(FRM_Request::get('id'));
		$modelGarage = new Garage();
		$garage = $modelGarage->getGarage($garage_id);
		
		$filter['garage_id'] = $garage_id;
		$modelImage = new Image();
		$image = $modelImage->getList($filter, false);
		
		$smarty->assign('image', $image);
		$smarty->assign('garage', $garage);
		$smarty->display('public/detail.html');
	}
	
	public function searchAction(){
		$smarty = FRM::load('smarty');
		$url_base = APPLICATION_URL.'/garage/search';
			
		if(FRM_Request::get('category')){
			$filter['category_id'] = trim(FRM_Request::get('category'));
			$url_base .= '/category/'.$filter['category_id'];
		}
		
		$modelGarage = new Garage();
		$garage = $modelGarage->getListSearch($filter);
		$marker = $modelGarage->getList($filter, false);
		
		if(FRM_Request::get('act')){
			if(FRM_Request::get('radius'))
				$filter['radius'] = trim(FRM_Request::get('radius'));
			if(FRM_Request::get('lat'))
				$filter['lat'] = trim(FRM_Request::get('lat'));
			if(FRM_Request::get('lng'))
				$filter['lng'] = trim(FRM_Request::get('lng'));

			$garage = $modelGarage->getGarageNearBy($filter);
			$marker = $modelGarage->getGarageNearBy($filter);
		}
		
		$smarty->assign('category_id', $filter['category_id']);
		$smarty->assign('url_base', $url_base);
		$smarty->assign('marker', $marker);
		$smarty->assign('garage', $garage);
		$smarty->display('public/search.html');
	}
	
	public function chooseAction(){
		$smarty = FRM::load('smarty');
		
		$garage_id = trim(FRM_Request::get('id'));
		$modelGarage = new Garage();
		$garage = $modelGarage->getGarage($garage_id);
		
		if(FRM_Request::get('act')){
			$fullname = trim(FRM_Request::getString('fullname'));
			$email = trim(FRM_Request::getString('email'));
			$address = trim(FRM_Request::getString('address'));
			$phone = trim(FRM_Request::getIntNumber('phone'));
			$desc = trim(FRM_Request::getString('desc'));
			$garage_id = trim(FRM_Request::getIntNumber('garage_id'));
			
			$visitor = new Visitor();
			$visitor->fullname = $fullname;
			$visitor->email = $email;
			$visitor->phonenumber = $phone;
			$visitor->address = $address;
			$visitor->garage_id = $garage_id;
			$visitor->description = $desc;
			$visitor->date_crate = date('Y-m-d H:i:s');
			
			if($visitor->insert()){
				$smarty->display('public/success.html');
				exit();
			}else{
				$smarty->display('public/unsuccess.html');
				exit();
			}
		}

		$smarty->assign('garage', $garage);
		$smarty->display('public/choose.html');
	}
	
	public function updateAction(){
		$smarty = FRM::load('smarty');
		$cur_login_user = unserialize(FRM_Session::get('user_serialize'));
		
		$garage_id = FRM_Request::get('id');
		$modelGarage = new Garage();
		$garage = $modelGarage->getDetailGarage($garage_id);
		
		$modelCat = new Category();
		$category = $modelCat->getList();
		$modelPackage = new Package();
		$package = $modelPackage->getList();
		$modelAd = new Advertisement();
		$advertise = $modelAd->getList();
		
		if(FRM_Request::get('act')){
			$title = trim(FRM_Request::get('title'));
			$category = trim(FRM_Request::get('category'));
			$package = trim(FRM_Request::get('package'));
			$advertise = trim(FRM_Request::get('advertise'));
			$link = trim(FRM_Request::get('link'));
			$desc = trim(FRM_Request::get('desc'));
			
			$garage->title = $title;
			$garage->category_id = $category;
			$garage->package_id = $package;
			$garage->ad_id = $advertise;
			$garage->link = $link;
			$garage->description = $desc;
			
			$garage->update();
			FRM::redirect(APPLICATION_URL.'/garage');
		}
		
		$smarty->assign('garage', $garage);
		$smarty->assign('category', $category);
		$smarty->assign('package', $package);
		$smarty->assign('advertise', $advertise);
		$smarty->assign('current_user', $cur_login_user);
		$smarty->display('public/update-garage.html');
	}
	
	public function imageAction(){
		$smarty = FRM::load('smarty');
		$cur_login_user = unserialize(FRM_Session::get('user_serialize'));
		
		$modelImage = new Image();
		$image = $modelImage->getList();
		$garage_id = FRM_Request::get('id');
		
		if(FRM_Request::get('action') == 'add'){
			if(FRM_Request::get('act')){
				$garage_id = FRM_Request::get('garage_id');
				$target_upload = STATIC_DIR."images/garage/";
				
				if(isset($_FILES['image']['name']) && $_FILES['image']['name']){
					
					$target_path = $target_upload . basename( $_FILES['image']['name']);
					move_uploaded_file($_FILES['image']['tmp_name'], $target_path);
				
					$modelImage->path = $target_path;
					$modelImage->name = $_FILES['image']['name'];
					$modelImage->garage_id = $garage_id;
					$modelImage->insert();
				
					FRM::redirect(APPLICATION_URL.'/garage/image/action/add/id/'.$garage_id);
				}
			}
			
			$smarty->assign('garage_id', $garage_id);
			$smarty->assign('image', $image);
			$smarty->assign('current_user', $cur_login_user);
			$smarty->display('public/addimage.html');
		}elseif (FRM_Request::get('action') == 'delete'){
			$image_id = FRM_Request::get('id');
			$modelImage->get($garage_id);
			if($modelImage->id){
				@unlink ( STATIC_DIR.'images/garage/'.$modelImage->name);
				$modelImage->delete();
			}
			
			FRM::redirect(APPLICATION_URL.'/garage');
		}

	}
}
?>